package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.AreaService;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.mapper.AreaMapper;
import com.yixun.qfbao.model.Area;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.Dictionary;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.AreaDto;
import com.yixun.qfbao.model.dto.AreaResultDto;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.MechanismAccountTypeEnum;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author guifang.weng
 * @create 2017-11-23 14:01
 **/
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private PartnerInfoService partnerInfoService;

    @Override
    public GetProvinceResDto getProvince() {
        GetProvinceResDto resDto = new GetProvinceResDto();
        List<Area> areaList = areaMapper.selectByParent();
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setAreaList(areaList);
        return resDto;
    }

    @Override
    public GetCityResDto getCity(GetCityReqDto getCityReqDto) {
        GetCityResDto resDto = new GetCityResDto();
        List<Area> areaList = areaMapper.selectChildren(getCityReqDto.getAreaId());
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setAreaList(areaList);
        return resDto;
    }

    @Override
    public GetAreaResDto getArea(GetAreaReqDto getCityReqDto) {
        GetAreaResDto resDto = new GetAreaResDto();
        List<Area> areaList = areaMapper.selectChildren(getCityReqDto.getAreaId());
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setAreaList(areaList);
        return resDto;
    }

    @Override
    public GetProvinceForOtherResDto getProvinceForOther() {
        GetProvinceForOtherResDto resDto = new GetProvinceForOtherResDto();
        List<Dictionary> list = new ArrayList<Dictionary>();
        List<Area> areaList = areaMapper.selectByParent();
        for(Area a : areaList){
            Dictionary dic = new Dictionary();
            dic.setId(a.getId());
            dic.setDicName(a.getName());
            list.add(dic);
        }
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setData(list);
        return resDto;
    }

    @Override
    public GetCityForOtherResDto getCity(GetCityForOtherReqDto dto){
        GetCityForOtherResDto resDto = new GetCityForOtherResDto();
        List<Dictionary> list = new ArrayList<Dictionary>();
        List<Area> cityList = areaMapper.selectCityForGroup();
        for(Area a : cityList){
            Dictionary dic = new Dictionary();
            dic.setId(a.getId());
            dic.setDicName(a.getName());
            dic.setDicType(a.getPinYin());
            list.add(dic);
        }
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setData(list);
        return resDto;
    }

    @Override
    public GetAreaForOtherResDto getArea(GetAreaForOtherReqDto dto){
        GetAreaForOtherResDto resDto = new GetAreaForOtherResDto();
        List<AreaResultDto> list = new ArrayList<AreaResultDto>();
        List<Area> areaList = areaMapper.selectChildren(dto.getParentId());
        for(Area a :areaList){
        		AreaResultDto dic = new AreaResultDto();
            dic.setId(a.getId());
            dic.setName(a.getName());
            list.add(dic);
        }
        resDto.setData(list);
        return resDto;
    }

    @Override
    public GetPremisesAreaListResDto getAreaAndPremisesCount(GetAreaAndPremisesCountReqDto getAreaAndPremisesCountReqDto) {
        GetPremisesAreaListResDto resDto = new GetPremisesAreaListResDto();

        String domain = getAreaAndPremisesCountReqDto.getDomain();
        String companyUuid = getAreaAndPremisesCountReqDto.getCompanyUuid();
        PartnerInfo partnerInfo = null;
        if(getAreaAndPremisesCountReqDto.getUserId() != null){
            partnerInfo = partnerInfoService.selectById(Long.parseLong(getAreaAndPremisesCountReqDto.getUserId()));
        }
//        if(partnerInfo == null){
//            throw new TServerException("未获取登陆人员信息！");
//        }
        if (StringUtils.isNotBlank(domain) && StringUtils.isNotEmpty(companyUuid)) {
            throw new TServerException("获取公司信息失败！");
        }
        CompanyDto entity = new CompanyDto();
        entity.setDomain(domain);
        entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
        entity.setCompanyUuid(companyUuid);
        entity.setStatus("2");
        Company company = companyService.selectByDto(entity);
        if (company == null) {
            throw new TServerException("获取公司信息失败！");
        }
        List<AreaDto> areaList = new ArrayList<>();
        //如果为案场助手
        if(SystemIdEnum.ASSISTANT.getCode().equals(getAreaAndPremisesCountReqDto.getLoginType())){
            //如果是管理员则获取全部
            if(MechanismAccountTypeEnum.mechanism.getNo().equals(partnerInfo.getAccountType())){
                areaList  = areaMapper.getPremisesAreaList(getAreaAndPremisesCountReqDto.getCityId(), company.getId());
            }else{
                areaList = areaMapper.getPremisesAreaListForCaseMember(getAreaAndPremisesCountReqDto.getCityId(), company.getId(),partnerInfo.getId());
            }
        }else if(SystemIdEnum.AGENCY_h5.getCode().equals(getAreaAndPremisesCountReqDto.getLoginType())){
            if(partnerInfo != null){
                areaList = areaMapper.getPremisesAreaListForAgency(getAreaAndPremisesCountReqDto.getCityId(), company.getId(),partnerInfo.getHigherid());
            }else{
                areaList  = areaMapper.getPremisesAreaList(getAreaAndPremisesCountReqDto.getCityId(), company.getId());
            }
        }else if(SystemIdEnum.WEAPP_AGENCY.getCode().equals(getAreaAndPremisesCountReqDto.getLoginType())) {
        	if(partnerInfo != null){
        		areaList = areaMapper.getPremisesAreaAndCityList(company.getId(),partnerInfo.getHigherid());
        	}
        }

        List<PremisesArea> list = new ArrayList<>();
        for(AreaDto areaDto :areaList){
            PremisesArea dic = new PremisesArea();
            dic.setAreaId(areaDto.getId());
            dic.setAreaName(areaDto.getName());
            dic.setPremisesCount(areaDto.getPremisesCount());
            dic.setParentId(areaDto.getParent());
            dic.setAreaFlag(areaDto.getAreaFlag());
            if(areaDto.getPremisesCount().longValue() > 0){
                list.add(dic);
            }
        }
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setPremisesAreaList(list);
        return resDto;
    }

    @Override
    public List<Area> getCommonArea(Long parentId, String name) {
        // parentId == 0: 获取省份
        // parentId != 0 并且有值: 获取对应的城市或区域
        // Objects.isNull(parentId): 根据 name 获取对应的区域
        List<Area> areaList = new ArrayList<>();
        if(Objects.nonNull(parentId)){
            if(parentId == 0){
                areaList = areaMapper.selectByParent();
            }else{
                areaList = areaMapper.selectChildren(parentId);
            }
        }else{
            // 通过名称查询,先获取名称对应的城市,再获取到城市对应的区域
            // 如果需要查询城市,则需通过parentId(省份),来获取
            if(StringUtils.isBlank(name)){
                return areaList;
            }
            List<Area> cityArea = areaMapper.selectAreaByHanZi(name);
            if(CollectionUtils.isEmpty(cityArea)){
                return areaList;
            }
            Area area = cityArea.get(0);
            areaList = areaMapper.selectChildren(area.getId());
        }
        return areaList;
    }
}
